package com.lanou.connotationjoke.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.activity.LoginActivity;
import com.lanou.connotationjoke.activity.MainActivity;
import com.lanou.connotationjoke.adapter.HomePagerAdapter;
import com.lanou.connotationjoke.entity.CurrentItemEty;
import com.lanou.connotationjoke.entity.LoginUserEty;
import com.lanou.connotationjoke.tool.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by dllo on 16/1/8.
 */
public class HomeFragment extends BaseFragment implements View.OnClickListener {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ImageButton imageButton;
    private HomePagerAdapter adapter;
    private List<Fragment> fragments;
    private TextView textView;
    private View toolBarImage;
    private View toolBarText;
    private View lable;
    private String[] titles = {"推荐","视频","图片","段子","订阅","精华","同城","收藏"};
    private Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8;
    private List<View> tabs;
    private int lastPosition = 0;
    private SharedPreferences sharedPreferences;

    private NetworkImageView loginDownIv;
    private NetworkImageView loginUpIv;
    private ImageView writeIv;
    private ImageView refreshIv;



    private Animation animation;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        tabLayout = (TabLayout) view.findViewById(R.id.tablayout_home);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager_home);
        imageButton = (ImageButton) view.findViewById(R.id.button_home);
        textView = (TextView) view.findViewById(R.id.tv_toolbar);
        toolBarImage = view.findViewById(R.id.layout_toolbar_image);
        toolBarText = view.findViewById(R.id.layout_toolbar_text);
        lable = view.findViewById(R.id.layout_lable);
        btn1 = (Button) view.findViewById(R.id.tab_1);
        btn2 = (Button) view.findViewById(R.id.tab_2);
        btn3 = (Button) view.findViewById(R.id.tab_3);
        btn4 = (Button) view.findViewById(R.id.tab_4);
        btn5 = (Button) view.findViewById(R.id.tab_5);
        btn6 = (Button) view.findViewById(R.id.tab_6);
        btn7 = (Button) view.findViewById(R.id.tab_7);
        btn8 = (Button) view.findViewById(R.id.tab_8);
        loginDownIv = (NetworkImageView) view.findViewById(R.id.iv_home_to_login_down);
        loginUpIv = (NetworkImageView) view.findViewById(R.id.iv_home_to_login_up);
        writeIv = (ImageView) view.findViewById(R.id.iv_home_to_write);
        refreshIv = (ImageView) view.findViewById(R.id.iv_refresh);
        tabs = new ArrayList<>();
        tabs.add(btn1);
        tabs.add(btn2);
        tabs.add(btn3);
        tabs.add(btn4);
        tabs.add(btn5);
        tabs.add(btn6);
        tabs.add(btn7);
        tabs.add(btn8);
        btn1.setBackgroundColor(Color.GREEN);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        EventBus.getDefault().register(this);

        textView.setText("推荐");
        sharedPreferences = getActivity().getSharedPreferences("userData",getActivity().MODE_PRIVATE);
        String iconUrl = sharedPreferences.getString("icon", "");
        loginDownIv.setDefaultImageResId(R.mipmap.ic_discovery_default_avatar);
        loginDownIv.setErrorImageResId(R.mipmap.ic_discovery_default_avatar);
        loginDownIv.setImageUrl(iconUrl, VolleySingleton.getImageLoader());


        loginUpIv.setDefaultImageResId(R.mipmap.ic_discovery_default_avatar);
        loginUpIv.setErrorImageResId(R.mipmap.ic_discovery_default_avatar);
        loginUpIv.setImageUrl(iconUrl, VolleySingleton.getImageLoader());




        fragments = new ArrayList<>();
        fragments.add(new HomeRecommendationFragment());
        fragments.add(new HomeVideoFragment());
        fragments.add(new HomePictureFragment());
        fragments.add(new HomeJokeFragment());
        fragments.add(new HomeSubscriptionFragment());
        fragments.add(new HomeEliteFragment());
        fragments.add(new HomeLocationFragment());
        fragments.add(new HomeCollectFragment());
        adapter = new HomePagerAdapter(getChildFragmentManager(),fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        //给按键注册监听
        imageButton.setOnClickListener(this);
        loginDownIv.setOnClickListener(this);
        loginUpIv.setOnClickListener(this);
        writeIv.setOnClickListener(this);
        refreshIv.setOnClickListener(this);



        tabLayout.setSelectedTabIndicatorColor(Color.HSVToColor(100, new float[]{200, 0, 0}));
        tabLayout.setSelectedTabIndicatorHeight(135);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                textView.setText(titles[position]);
                tabs.get(lastPosition).setBackgroundColor(Color.BLACK);
                tabs.get(position).setBackgroundColor(Color.GREEN);
                lastPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

}

    //当通过Intent跳离当前Activity时会执行onStop()方法,如果finish()当前的Activity会执行onDestory()方法
    //而通过Intent调到LoginActivity后需要同过EventBus向当前的fragment页面传值操作因此不能取消注册
    //因此不能再onStop()生命周期中取消注册
    //注释:返回时是通过onBackPress()方法返回该Fragment不会在执行一遍onCreate()等生命周期,因此不会重复注册
    //而通过Intent调回该界面时会再次执行onCreate()等生命周期
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_home_to_login_down:
                Intent intent = new Intent(getActivity(),LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_home_to_login_up:
                Intent intent1 = new Intent(getActivity(),LoginActivity.class);
                startActivity(intent1);
                break;
            case R.id.iv_home_to_write:
                break;
            case R.id.button_home:
                tabLayout.setScrollPosition(7, 0, true);
                break;
            case R.id.iv_refresh:
                int pos = viewPager.getCurrentItem();
                if (pos<4) {
                    CurrentItemEty currentItemEty = new CurrentItemEty();
                    currentItemEty.setI(pos);
                    EventBus.getDefault().post(currentItemEty);
                    animation = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_refresh_icon);
                    refreshIv.setAnimation(animation);
                }
                break;

        }

    }


    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onMainEvent(String s) {
        if (s.equals("down")){
            lable.setVisibility(View.VISIBLE);
            toolBarImage.setVisibility(View.VISIBLE);
            toolBarText.setVisibility(View.GONE);

        }else if (s.equals("up")){
            lable.setVisibility(View.GONE);
            toolBarImage.setVisibility(View.GONE);
            toolBarText.setVisibility(View.VISIBLE);
        }else if (s.equals("stop")){
            animation = null;
            refreshIv.setAnimation(animation);
        }
    }


    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onMainEvent(LoginUserEty loginUserEty) {
        loginUpIv.setImageUrl(loginUserEty.getIconUrl()
                , VolleySingleton.getImageLoader());
        loginDownIv.setImageUrl(loginUserEty.getIconUrl()
                ,VolleySingleton.getImageLoader());
    }

}
